package com.fingard.dsp.bank.directbank.aibank01;

import com.alibaba.fastjson.JSON;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fingard.dsp.bank.directbank.aibank01.request.SendRequest;
import com.fingard.dsp.bank.directbank.bankInterface.IAuditAccount;
import com.fingard.dsp.bank.reqrespobj.auditAccount.ReqAuditAccount;
import com.fingard.dsp.bank.reqrespobj.auditAccount.RetAtsAuditAccountQry;
import com.fingard.dsp.bank.reqrespobj.auditAccount.RetAtsAuditAccountQryDetail;
import com.kayakwise.sgp.object.request.cnaps.SeLiqu0001Req;
import com.kayakwise.sgp.object.request.cnaps.SeLiqu0002Req;
import com.kayakwise.sgp.object.response.cnaps.SeLiqu0001Resp;
import com.kayakwise.sgp.object.response.cnaps.SeLiqu0002Resp;

/**
 * 
 * @author xtt
 * @date 2019年6月17日
 */
public class AIBANK01AuditAccount extends AIBANK01Base implements IAuditAccount{

	@Override
	public RetAtsAuditAccountQry retAtsAuditAccountQry(ReqAuditAccount p_req) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:dsp-config/spring-config.xml");
	    SendRequest client1 = (SendRequest) ac.getBean("SendRequest");
		RetAtsAuditAccountQry retAts = new RetAtsAuditAccountQry(p_req.reqHeader);
		RetAtsAuditAccountQryDetail detail = new RetAtsAuditAccountQryDetail();
		boolean isSuccess = false;
		//清算类交易申请
		//构建请求model
		SeLiqu0001Req request0001 = new SeLiqu0001Req();
		//01 - 清算账户信息查询申请  02 - 净借记限额查询申请  03 - 净借记限额圈存资金调增/调减申请
		request0001.setReq_type("02");
		request0001.setQrytp("QT00");
		WriteBankLogLn("开始请求银行接口，参数值" + request0001.toString());
		System.out.println("开始请求银行接口，参数值" + request0001.toString());
		SeLiqu0001Resp resp0001 = client1.sendToBank(request0001);
		if(resp0001!=null) {
			System.out.println("银行返回结果头部状态码：" + resp0001.getHead().getRetStatus());
			System.out.println("银行返回结果头部信息：" + resp0001.getHead().getRetResult());
			String msgInfo = resp0001.getStep_status();
			WriteBankLogLn("银行返回结果信息：" + resp0001.toString());
			System.out.println("银行返回结果状态码：" + resp0001.getStep_status());
			if(msgInfo!=null && msgInfo.equals("S00")) {
				isSuccess = true;
				System.out.println("清算类交易申请成功，开始处理清算类交易申请结果查询。。。");
				WriteBankLogLn("清算类交易申请成功，开始处理清算类交易申请结果查询。。。");
			}
		}
		//清算类交易申请结果查询
		if(isSuccess) {
			SeLiqu0002Req request002 = new SeLiqu0002Req();
			//原全局流水号
			request002.setOri_busi_serno(request0001.getHead().getTransId());
			//原请求日期
			request002.setOri_req_date(request0001.getHead().getSysDate());
			//原子交易序号
			request002.setOri_sys_serno(request0001.getHead().getSubTransSeq());
			//交易类型
			request002.setReq_type("02");
			WriteBankLogLn("请求的对象参数" + JSON.toJSONString(request002));
			SeLiqu0002Resp resp0002 = client1.sendToBank(request002);
			WriteBankLogLn("返回的对象结果seLiqu0002Resp：" + JSON.toJSONString(resp0002));
			if(resp0002!=null) {
				System.out.println("银行返回结果信息：" + resp0002.toString());
				String tmpRetCode = resp0002.getStep_status();
	            String tmpRetInfo = resp0002.getRjctinf();
	            if(tmpRetCode.equals("I05")){    
	                    detail.mmbid = resp0002.getMmbid();
	                    detail.cdtld02 = String.valueOf(resp0002.getCdtld02());
	                    detail.ttlpldgval = String.valueOf(resp0002.getTtlpldgval());
	                    detail.cdtxtnsnlmt = String.valueOf(resp0002.getCdtxtnsnlmt());
	                    detail.netbtlmt = String.valueOf(resp0002.getNetbtlmt());
	                    detail.avlblnetdbt = String.valueOf(resp0002.getAvlblnetdbt());
	                    retAts.writeRetDetail(detail);
	            }
	            else{
	                retAts.writeStart();
	                retAts.writeRespCode("-1");
	                retAts.writeRespInfo(tmpRetInfo);
	            }
	        }
	        else{
	            retAts.writeStart();
	            retAts.writeRespCode("-1");
	            retAts.writeRespInfo("交易失败");
	        }
		}else{
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("交易失败");
        }
	      retAts.writeEnd();
		  return retAts;
	}

}
